var app = getApp();

Page({
  data: {
    address_list: null,
    parentId: 1,
    page: 1,
    rows: 1000,
    id: ''
  },
  onLoad: function(e) {
    this.data.id = wx.getStorageSync("bizUserInfo").id
    console.log(this.data.id)
  },
  onReady: function() {},
  onShow: function(e) {
    var that = this
    app.getData({
      url: app.api.selectUserAdd(),
      method: 'GET',
      data: {
        hyId: that.data.id
      },
      ok: function(res) {
        console.log(res);
        that.setData({
          address_list: res.data.list
        })
      }
    })
  },
  form_save: function(e) {
    console.log(e);
    var t = e.detail.formId;
    // app.getData({
    //   url: app.api.seveUserAddress(),
    //   method: 'POST',
    //   data: {
    //     detailInfo: i,
    //     provinceName: n,
    //     hyName: t,
    //     hyId: a,
    //     telNumber: o,
    //     isSend: 1,
    //     isDefault: ''
    //   },
    //   ok: function (res) {
    //     console.log(res),
    //       "1" == res.data.code && (wx.showToast({
    //         title: '保存成功',
    //         duration: 1e3
    //       }))
    //     setTimeout(function () {
    //       wx.navigateBack({
    //         delta: 1
    //       });
    //     }, 1e3)
    //   }
    // })
  },
  bianji: function(e) {
    var t = e.currentTarget.dataset.bjid;
    console.log(t), wx.navigateTo({
      url: "editAddress?editAddress=" + t,
      success: function(e) {},
      fail: function(e) {},
      complete: function(e) {}
    });
  },
  shanchu: function(e) {
    var that = this;
    console.log(e)
    console.log(e.currentTarget.dataset.scid);
    var t = e.currentTarget.dataset.scid;
    wx.showModal({
      title: "提示",
      content: "确定要删除该地址吗？",
      confirmText: "确定",
      cancelText: "取消",
      success: function(e) {
        console.log(e),
          e.confirm ? (app.getData({
              url: app.api.deleteUserAdd(),
              method: 'GET',
              data: {
                id: t
              },
              ok: function(res) {
                console.log(res);
                "1" == res.data.code && (wx.showToast({
                  title: '删除成功',
                }))
                that.onShow();
              }
            }),
            console.log("用户点击确定")) : console.log("用户点击取消");
      }
    });
  },
  radioChange: function(e) {
    console.log(this.data.address_list)
    console.log(e)
    var that = this,
      customId = wx.getStorageSync('bizUserInfo').id;
    console.log("radio发生change事件，携带value值为：", e.detail.value);
    var o = e.detail.value;
    app.getData({
      url: app.api.updataUserAdd(),
      method: 'POST',
      data: {
        id: o,
        isDefault: 1,
        hyId:customId
      },
      ok: function(e) {
        console.log(e);
        that.onShow();
        "1" == e.data.code && (wx.showToast({
            title: '修改成功',
            duration: 1e3
          }),
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 1e3))
      }
    })
  },
  getWechatAddress: function(e) {
    console.log(e)
    var that = this;
    wx.chooseAddress({
      success: function(res) {
        console.log(res),
          res.errMsg == "chooseAddress:ok" && (wx.showLoading(),
          app.getData({
            url: app.api.seveUserAddress(),
            method: 'POST',
            data: {
              detailInfo: res.detailInfo,
              provinceName: res.provinceName,
              countyName: res.countyName,
              cityName: res.cityName,
              hyName: res.userName,
              hyId: that.data.id,
              telNumber: res.telNumber,
              isSend: 1,
              isDefault: 0
            },
            ok: function (res) {
              console.log(res),
                "1" == res.data.code && (wx.showToast({
                  title: '保存成功',
                  duration: 1e3
                }),that.onShow())
            }
          })
          )
      },
      fail: function() {
        wx.getSetting({
          success: function(e) {
            console.log(e), e.authSetting["scope.address"] ? console.log("取消") : wx.showModal({
              title: "提示",
              content: "您拒绝了获取收货地址授权，部分功能无法使用,点击确定重新获取授权。",
              showCancel: !1,
              success: function(e) {
                e.confirm && wx.openSetting({
                  success: function(e) {
                    e.authSetting["scope.address"] && o.getWechatAddress();
                  },
                  fail: function(e) {}
                });
              }
            });
          }
        });
      }
    });
  },
  pickAddress:function(e){
    console.log(e)
    var adId = e.currentTarget.dataset.id,
    pages = getCurrentPages(),
    currPage = pages[pages.length - 1],   //当前页面
    prevPage = pages[pages.length - 2];  //上一个页面
    console.log(pages)
    prevPage.setData({
      adId: adId
    });
    wx.navigateBack({
      delta: 1
    });
  }

});